﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using jsb;
using System.IO;

public class Config : MonoBehaviour {

	// --------------- 坦克信息 -----------------------------

	public class TankConfig {
		/// <summary>
		/// 坦克移动速度
		/// </summary>
		public float[] Speed = new float[] { 3.75f, 7.5f, 3.75f, 3.75f, 3.75f, 7.5f, 7.5f, 3.75f, 7.5f, 7.5f }; 

		/// <summary>
		/// 坦克攻击间隔，间隔多少秒
		/// </summary>
		public float[] AttackInterval = new float[] { 1f, 0.5f, 0.7f, 0.7f, 1f, 0.5f, 0.5f, 1f, 0.5f, 0.5f };

		/// <summary>
		/// 只有敌方有血量的概念
		/// </summary>
		public int[] Blood = new int[] { 1, 1, 2, 4, 1, 1, 1, 1, 1, 1 };
	}
	public static TankConfig tankConfig = new TankConfig();

	// --------------- 子弹信息 ------------------------------ 

	public class BulletConfig {
		/// <summary>
		/// 子弹飞行速度
		/// </summary>
		public float[] Speed = new float[] { 10f, 20f, 15f, 15f, 10f, 20f, 20f, 10f, 20f, 20f };

		/// <summary>
		/// 子弹攻击力
		/// </summary>
		public int[] ATT = new int[] { 1, 1, 1, 1, 1, 1, 2, 1, 1, 2};
	}
	public static BulletConfig bulletConfig = new BulletConfig();

	// ---------------- 食物信息 -----------------------------

	public class FoodConfig {
		/// <summary>
		/// 食物效果持续时间，食物顺序是：无敌（闪电）、加命（医疗箱）、升级（星星）、基地防御（盾牌）、炸弹（）、停止（锁）
		/// 该属性对 加命、升级、炸弹 无效
		/// </summary>
		public float[] EffectivenessDuration = new float[] { 15f, 0, 0, 15f, 0, 15f};

		/// <summary>
		/// 没被吃掉的情况下，食物存在最长时间
		/// </summary>
		public float[] Duration = new float[] { 15f, 15f, 15f, 15f, 15f, 15f}; 

		/// <summary>
		/// 食物闪烁频率，FlickerTimer 帧，闪烁一次
		/// </summary>
		public int FlickerTimer = 50;

		/// <summary>
		/// 滑块带来的滑动速度
		/// </summary>
		public float SlideSpeed = 10f;
	}
	public static FoodConfig foodConfig = new FoodConfig(); 

	// ---------------- 游戏信息 -----------------------------

	public class GameConfig {
		/// <summary>
		/// 关卡数量，关卡编号从 1 ~ ChapterNum，关卡地图文件由 1.txt ~ ChapterNum.txt
		/// </summary>
		public int ChapterNum = 7;

		/// <summary>
		/// 坦克刚出生的无敌时间长度
		/// </summary>
		public float InvincibleTimeOfBirth = 4f;

		/// <summary>
		/// 友方攻击惩罚时间
		/// </summary>
		public float FriendlyAttackPenaltyTime = 5f;

		/// <summary>
		/// 坦克被友方攻击，闪烁的频率，每 FlickerTimer 帧闪烁一次
		/// </summary>
		public int FlickerTimer = 50;

		/// <summary>
		/// 初始玩家坦克生命数量
		/// </summary>
		public int LifeNum = 3;

		/// <summary>
		/// 四种敌方坦克分值
		/// </summary>
		public int[] EnemyScore = new int[] { 100, 200, 300, 400 };

		/// <summary>
		/// 生成敌人的间隔，单位 秒
		/// </summary>
		public float GenEnemyTimer = 7f;
	}
	public static GameConfig gameConfig = new GameConfig();

	// --------------- 关卡信息 -----------------------------

	public int Chapter;        // 第几关
	public int Player1;        // 玩家一当前坦克类型
	public int Player2;        // 玩家二当前坦克类型
	public int Player1Num;     // 玩家一剩余生命
	public int Player2Num;     // 玩家二剩余生命
	public int Score1;         // 玩家一分数
	public int Score2;         // 玩家二分数
	public int Num;            // 游戏人数

	public int Status;         // 0 : gameover  1 : pass
	public int[,] ScorePoint;  // 4 行 2 列   ScorePoint[i, j] 表示 第 j 名玩家消灭第 i 类型的敌人多少只     

	void Awake() {
		ScorePoint = new int[4, 2];

		StartCoroutine ("Read1");
		StartCoroutine ("Read2");
	    StartCoroutine ("Read3");
		StartCoroutine ("Read4");
	}

	IEnumerator Read1()	
	{
		var p = "file:///" + Application.streamingAssetsPath + "/Config/TankConfig.xml";
		var w = new WWW (p);
		yield return w;
		string text = w.text;
		tankConfig = XmlUtils.ToObject<TankConfig> ( text );
	}

	IEnumerator Read2()
	{
		var p = "file:///" + Application.streamingAssetsPath + "/Config/BulletConfig.xml";
		var w = new WWW (p);
		yield return w;
		string text = w.text;
		bulletConfig = XmlUtils.ToObject<BulletConfig> ( text );
	}

	IEnumerator Read3()
	{
		var p = "file:///" + Application.streamingAssetsPath + "/Config/FoodConfig.xml";
		var w = new WWW (p);
		yield return w;
		string text = w.text;
		foodConfig = XmlUtils.ToObject<FoodConfig> ( text );
	}

	IEnumerator Read4()
	{
		var p = "file:///" + Application.streamingAssetsPath + "/Config/GameConfig.xml";
		var w = new WWW (p);
		yield return w;
		string text = w.text;
		gameConfig = XmlUtils.ToObject<GameConfig> ( text );
	}
		
	public void Save() {
		PlayerPrefs.SetInt ("Chapter", Chapter);
		PlayerPrefs.SetInt ("Player1", Player1);
		PlayerPrefs.SetInt ("Player1Num", Player1Num);
		PlayerPrefs.SetInt ("Player2", Player2);
		PlayerPrefs.SetInt ("Player2Num", Player2Num);
		PlayerPrefs.SetInt ("Score1", Score1);
		PlayerPrefs.SetInt ("Score2", Score2);
		PlayerPrefs.SetInt ("Num", Num);
		PlayerPrefs.Save ();
	}

	public bool Load() {
		if (PlayerPrefs.HasKey ("Chapter")) {
			Chapter = PlayerPrefs.GetInt ("Chapter");
			Player1 = PlayerPrefs.GetInt ("Player1");
			Player1Num = PlayerPrefs.GetInt ("Player1Num");
			Player2 = PlayerPrefs.GetInt ("Player2");
			Player2Num = PlayerPrefs.GetInt ("Player2Num");
			Score1 = PlayerPrefs.GetInt ("Score1");
			Score2 = PlayerPrefs.GetInt ("Score2");
			Num = PlayerPrefs.GetInt ("Num");
			return true;
		} 
		return false;
	}


	public void Update() {
	
	}
}
